package cn.wolfcode.trip.base.util;

import cn.wolfcode.trip.base.domain.BaseDomain;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map;

public class JSONUtil {
    private static ObjectMapper mapper  = new ObjectMapper();

    public static String toJSONString(Object obj){
        try {
            return mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
    //head 就是map key 的前半部分  obj就是要转成json的对象  将属性已经属性值 变成 map
    private static void ToJsonMapByRecursion(String head,Object obj, Map<Object,Object> map) throws Exception {
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.class);
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor pd : pds) {
            //属性名不为json 且 属性值不为null 且代理类不行
            if (!("json".equals(pd.getName())) && pd.getReadMethod().invoke(obj)!=null && !("handler".equals(pd.getName()))) {
                //判断属性是否  是basedomain的子类或者本类 如果是就递归
                if(pd.getReadMethod().invoke(obj) instanceof BaseDomain){
                    ToJsonMapByRecursion(pd.getName(),pd.getReadMethod().invoke(obj),map);
                }else {
                    //head  是map中 key 的前半部分
                    if(head != null ){
                        //属性名大写
                        char[] ch = pd.getName().toCharArray();
                        if (ch[0] >= 'a' && ch[0] <= 'z') {
                            ch[0] = (char) (ch[0] - 32);
                        }
                        map.put(head + String.valueOf(ch), pd.getReadMethod().invoke(obj));
                    }else {
                        Object propertyValue = pd.getReadMethod().invoke(obj);
                        //处理时间时  要格式化 除非在domain里处理了
                       /* if( propertyValue instanceof Date){
                            //格式化 如果是时间 就格式化
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            propertyValue = sdf.format(propertyValue);
                        }*/
                        map.put(pd.getName(), propertyValue);
                    }
                }
            }
        }
    }
    public static String toJsonStringByRecursion(Object obj){
        HashMap<Object, Object> map = new HashMap<>();
        try {
            ToJsonMapByRecursion(null,obj,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJsonStringByJackSon(map);
    }
    public static String toJsonStringByJackSon(Object obj) {
        try {
            return mapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
    public static String toJsonStringByMap(Object obj) {
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.class);
            PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
            HashMap<String, Object> map = new HashMap<>();
            for (PropertyDescriptor pd : pds) {
                if (!("json".equals(pd.getName()))) {
                    map.put(pd.getName(), pd.getReadMethod().invoke(obj));
                }
            }
            return mapper.writeValueAsString(map);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}
